﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
namespace ERP.Classes
{
    public class cs_erp_hangtrungbayInfo
    {
        private int _id_trungbay;
        private int _id_chitiet;
        private double _soluong;
        private string _noidung;
        private string _ghichu;
        private DateTime _ngaythuchien;
        private string _ngaykt;
        private bool _trungbay;
        private bool _sualoi;

        public cs_erp_hangtrungbayInfo()
        {
            this._id_trungbay = 0;
            this._id_chitiet = 0;
            this._soluong = 0;
            this._noidung = " ";
            this._ghichu = " ";
            this._ngaythuchien = Convert.ToDateTime("01/01/9999");
            this._ngaykt = " ";
            this._trungbay = false;
            this._sualoi = false;

        }
        public int id_trungbay
        {
            get
            {
                return this._id_trungbay;
            }
            set
            {
                this._id_trungbay = value;
            }
        }
        public int id_chitiet
        {
            get
            {
                return this._id_chitiet;
            }
            set
            {
                this._id_chitiet = value;
            }
        }
        public double soluong
        {
            get
            {
                return this._soluong;
            }
            set
            {
                this._soluong = value;
            }
        }
        public string noidung
        {
            get
            {
                return this._noidung;
            }
            set
            {
                this._noidung = value;
            }
        }
        public string ghichu
        {
            get
            {
                return this._ghichu;
            }
            set
            {
                this._ghichu = value;
            }
        }
        public DateTime ngaythuchien
        {
            get
            {
                return this._ngaythuchien;
            }
            set
            {
                this._ngaythuchien = value;
            }
        }
        public string ngaykt
        {
            get
            {
                return this._ngaykt;
            }
            set
            {
                this._ngaykt = value;
            }
        }
        public bool trungbay
        {
            get
            {
                return this._trungbay;
            }
            set
            {
                this._trungbay = value;
            }
        }
        public bool sualoi
        {
            get
            {
                return this._sualoi;
            }
            set
            {
                this._sualoi = value;
            }
        }

        public int ExcuteStoreProcedure(string storeProcedure, int option)
        {
            string[] paraName = new string[10];
            paraName[0] = "@id_trungbay";
            paraName[1] = "@id_chitiet";
            paraName[2] = "@soluong";
            paraName[3] = "@noidung";
            paraName[4] = "@ghichu";
            paraName[5] = "@ngaythuchien";
            paraName[6] = "@ngaykt";
            paraName[7] = "@trungbay";
            paraName[8] = "@sualoi";
            paraName[9] = "@option";
            object[] paraValue = new object[10];
            paraValue[0] = this._id_trungbay;
            paraValue[1] = this._id_chitiet;
            paraValue[2] = this._soluong;
            paraValue[3] = this._noidung;
            paraValue[4] = this._ghichu;
            paraValue[5] = this._ngaythuchien;
            paraValue[6] = this._ngaykt;
            paraValue[7] = this._trungbay;
            paraValue[8] = this._sualoi;
            paraValue[9] = option;
            csCSDL csdl = new csCSDL();
            int i = 0;
            i = csdl.CreateParameters(paraName, 10, paraValue);
            return csdl.ExcuteStoreProcedure(storeProcedure);
        }
        public int ExcuteProcedure(int option)
        {
            return this.ExcuteStoreProcedure("Proc_ERP_HANGTRUNGBAY", option);
        }

        public void ShowDataInCombo(ComboBox cbo, string condition)
        {
            csComboBox cb = new csComboBox("id_chitiet", "id_trungbay");
            cb.KetDuLieu(cbo, "ERP_HANGTRUNGBAY", condition);
        }

        public int ShowDataGrid(DataGrid datagrid, string condition)
        {
            csDataGrid grid = new csDataGrid();

            string[] header = new string[9];
            header[0] = "id_trungbay";
            header[1] = "id_chitiet";
            header[2] = "soluong";
            header[3] = "noidung";
            header[4] = "ghichu";
            header[5] = "ngaythuchien";
            header[6] = "ngaykt";
            header[7] = "trungbay";
            header[8] = "sualoi";

            string[] mappingname = new string[9];
            mappingname[0] = "id_trungbay";
            mappingname[1] = "id_chitiet";
            mappingname[2] = "soluong";
            mappingname[3] = "noidung";
            mappingname[4] = "ghichu";
            mappingname[5] = "ngaythuchien";
            mappingname[6] = "ngaykt";
            mappingname[7] = "trungbay";
            mappingname[8] = "sualoi";

            int[] colwidth = new int[9];
            colwidth[0] = 12;
            colwidth[1] = 12;
            colwidth[2] = 24;
            colwidth[3] = 3000;
            colwidth[4] = 1500;
            colwidth[5] = 24;
            colwidth[6] = 300;
            colwidth[7] = 3;
            colwidth[8] = 3;
            csCSDL csdl = new csCSDL();
            return grid.CreateTableStyle("ERP_HANGTRUNGBAY", datagrid, csdl.OpenTable("ERP_HANGTRUNGBAY", condition), header, mappingname, colwidth, 9);
        }
        public int Get_Info()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                ds = csdl.OpenTable("ERP_HANGTRUNGBAY", "id_trungbay = " + this._id_trungbay);
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    return 0;
                }
                this._id_trungbay = Convert.ToInt32(ds.Tables[0].Rows[0]["id_trungbay"].ToString().Trim());
                this._id_chitiet = Convert.ToInt32(ds.Tables[0].Rows[0]["id_chitiet"].ToString().Trim());
                this._soluong = Convert.ToDouble(ds.Tables[0].Rows[0]["soluong"].ToString().Trim());
                this._noidung = ds.Tables[0].Rows[0]["noidung"].ToString().Trim();
                this._ghichu = ds.Tables[0].Rows[0]["ghichu"].ToString().Trim();
                this._ngaythuchien = Convert.ToDateTime(ds.Tables[0].Rows[0]["ngaythuchien"].ToString().Trim());
                this._ngaykt = ds.Tables[0].Rows[0]["ngaykt"].ToString().Trim();
                this._trungbay = Convert.ToBoolean(ds.Tables[0].Rows[0]["trungbay"].ToString().Trim());
                this._sualoi = Convert.ToBoolean(ds.Tables[0].Rows[0]["sualoi"].ToString().Trim());
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        public DataSet Get_List()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select a.id_trungbay, b.id_chitiet_nhapkho, "
                          + "  a.soluong as soluongtrungbay, a.noidung as noidungdieuchinh," 
                          + "  a.ngaythuchien, a.ngaykt, a.trungbay, a.sualoi,"
                          + "  k.tenkho, g.tennhomhh, h.donvitinh, f.mahh, f.tenhh, f.nguyenlieu, b.ngaynhap, c.noidung as noidungnhapkho,"
                          + "  b.soluong as soluongnhapkho, e.tenkhachhang, e.dienthoai as sodienthoai"
                          + "  from ERP_CHITIET_NHAPKHO b"
                          + "  left join ERP_HANGTRUNGBAY a on (a.id_chitiet = b.id_chitiet_nhapkho and (a.ngaykt = ' ' or (a.trungbay = 0 and a.sualoi = 0)))"
                          + "  left join ERP_NHAPKHO c on (c.id_lannhap = b.id_lannhap and c.trangthai = 1 and c.manx = 1)"
                          + "  left join ERP_HOADON d on (d.id_hd = c.id_hd and d.trangthai in (4, 5))"
                          + "  left join ERP_KHACHHANG e on (e.id_kh = d.id_kh)"
                          + "  left join ERP_HANGHOA f on (f.id_hh = b.mahh)"
                          + "  left join ERP_NHOMHANG g on (f.id_nhom = g.id_nhomhhh)"
                          + "  left join ERP_DVT h on (h.id_dvt = f.dvt)"
                          + "  left join ERP_KHOHANG k on (k.id_khohang = c.makho)"
                          + "  where b.soluong*b.nhan>0"
                          + "  order by f.tenhh asc";
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }
    }
}